package copyengine.utils
{
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import flash.geom.Vector3D;

	public final class IsometricUtils
	{
		private static var tempVector:Vector3D = new Vector3D();

		public function IsometricUtils()
		{
		}

		/**
		 * WARNINIG::
		 * 		this function will return an static variable , not each tile create one.
		 */
		private static var globalToIsoTempPoint:Point = new Point();

		public static function convertGlobalPosToIsoPos(_parentContainer:DisplayObjectContainer,
														_globalPosX:Number,
														_globalPosY:Number,
														_tileRealSize:int):Point
		{
			//change the mouse position to porjection coordinates.
			globalToIsoTempPoint.x = _globalPosX;
			globalToIsoTempPoint.y = _globalPosY;
			globalToIsoTempPoint = _parentContainer.globalToLocal(globalToIsoTempPoint);

			//change projection coordinate to isometric coordinates
			tempVector.x = globalToIsoTempPoint.x;
			tempVector.y = globalToIsoTempPoint.y;
			tempVector.z = 0;
			IsoMath.screenToIso(tempVector);

			//caulate the target col and row
			globalToIsoTempPoint.x = int(tempVector.x / _tileRealSize);
			globalToIsoTempPoint.y = int(tempVector.y / _tileRealSize);

			return globalToIsoTempPoint;
		}

		private static var isoToScreenTempPoint:Point = new Point();

		public static function convertIsoPosToScreenPos(_col:int, _row:int, _height:int, _tileRealSize:int):Point
		{
			tempVector.x = _col * _tileRealSize;
			tempVector.y = _row * _tileRealSize;
			tempVector.z = _height * _tileRealSize;
			IsoMath.isoToScreen(tempVector);
			isoToScreenTempPoint.x = tempVector.x;
			isoToScreenTempPoint.y = tempVector.y;
			return isoToScreenTempPoint;
		}

		public static function alignGlobalPosToIsoScreenPos(_parentContainer:DisplayObjectContainer,
															_globalPosX:Number, _globalPosY:Number, _tileRealSize:int):Point
		{
			var point:Point = convertGlobalPosToIsoPos(_parentContainer, _globalPosX, _globalPosY, _tileRealSize);
			point = convertIsoPosToScreenPos(point.x, point.y, 0, _tileRealSize);
			return point;
		}


	}
}
